/**
 * Copyright 2019 Inc.
 **/
package com.myz.service.gateway.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


/**
 * 自定义Filter 过滤器
 *
 * @author maoyz0621 on 19-3-14
 * @version: v1.0
 */
public class RequestTimeFilter implements GatewayFilter, Ordered {

    private static final Logger log = LoggerFactory.getLogger(GatewayFilter.class);

    private static final String REQUEST_TIME_BEGIN = "requestTimeBegin";

    /**
     * 在该方法中，先记录了请求的开始时间，并保存在ServerWebExchange中，此处是一个“pre”类型的过滤器，然后再chain.filter的内部类中的run()方法中相当于"post"过滤器，在此处打印了请求所消耗的时间
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        exchange.getAttributes().put(REQUEST_TIME_BEGIN, System.currentTimeMillis());
        return chain.filter(exchange).then(
                Mono.fromRunnable(() -> {
                    Long startTime = exchange.getAttribute(REQUEST_TIME_BEGIN);
                    if (null != startTime) {
                        log.info(exchange.getRequest().getURI().getRawPath() + " : " + (System.currentTimeMillis() - startTime) + "ms");
                    }
                })
        );
    }

    /**
     * 给过滤器设定优先级别的，值越大则优先级越低。
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
